package com.baibei.ebec.welfare.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baibei.ebec.follow.activity.FollowUserDetailActivity;
import com.baibei.ebec.home.BuildConfig;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.ebec.welfare.adapter.WelfareRankingAdapter;
import com.baibei.model.PercentInfo;
import com.baibei.model.WelfareRankInfo;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;
import com.baibei.widget.SerializableRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelfareRankingFragment extends Fragment {

    @BindView(R2.id.rv)
    RecyclerView rv;
    Unbinder unbinder;
    private String time;
    private IUserApi mUserApi;
    private WelfareRankingAdapter rankingAdapter;

    public static WelfareRankingFragment newInstance(String text, SerializableRefreshLayout refreshLayout) {
        WelfareRankingFragment wrf = new WelfareRankingFragment();
        Bundle bundle = new Bundle();
        bundle.putString("time", text);
        bundle.putSerializable("refreshlayout", refreshLayout);
        wrf.setArguments(bundle);
        return wrf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_welfare_ranking, container, false);
        unbinder = ButterKnife.bind(this, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
        init();
        return view;
    }

    private void init() {
        if (getArguments() == null) {
            return;
        }

        AppUI.loading(getContext());

        SerializableRefreshLayout serializableRefreshLayout = (SerializableRefreshLayout) getArguments().getSerializable("refreshlayout");
        final CommonRefreshLayout refreshLayout = serializableRefreshLayout.getRefreshLayout();

        //处理recyclerview滑动冲突
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (rv == null || rv.getChildCount() == 0) {
                    return;
                }
                RecyclerView.LayoutManager layoutManager = rv.getLayoutManager();
                int position = -1;
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    position = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                }
                refreshLayout.setEnabled(position == 0);
            }
        });

        time = getArguments().getString("time");

        Observable observable;
        switch (time) {
            case "day":
                observable = RxObservable.create(mUserApi.getWelfareRankInfoDay(), this.toString());
                break;
            case "week":
                observable = RxObservable.create(mUserApi.getWelfareRankInfoWeek(), this.toString());
                break;
            case "month":
                observable = RxObservable.create(mUserApi.getWelfareRankInfoMonth(), this.toString());
                break;
            default:
                observable = RxObservable.create(mUserApi.getWelfareRankInfoDay(), this.toString());
        }

        observable.subscribe(new ApiDefaultObserver<List<WelfareRankInfo>>() {
            @Override
            protected void onError(String s) {
                ToastUtils.showShortToast(s);
                AppUI.dismiss();
            }

            @Override
            protected void accept(final List<WelfareRankInfo> welfareRankInfos) {
                LogUtils.e("welfarerankinfos",welfareRankInfos.toString());
                LinearLayoutManager llm = new LinearLayoutManager(getContext());
                rv.setLayoutManager(llm);

                rankingAdapter = new WelfareRankingAdapter(welfareRankInfos);

                rv.setAdapter(rankingAdapter);
                rankingAdapter.setOnItemListener(new WelfareRankingAdapter.MyItemClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        Intent intent = new Intent(getActivity(), FollowUserDetailActivity.class);
//                        intent.putExtra("data",welfareRankInfos.get(position));
                        intent.putExtra("followerId",welfareRankInfos.get(position).getFollowerId());
                        startActivity(intent);
                    }
                });
                AppUI.dismiss();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
